package im

import (
    //"fmt"
	"encoding/json"
	"dhfshop/domain/im"
	"dhfshop/controllers/base"
	"thirdpaysdk/api/oceanpayment"
	"github.com/astaxie/beego"
	"dhfshop/domain/pay"
	"dhfshop/service/redisgo"
)

type OceanPayController struct {
	base.BaseController
}

func (this *OceanPayController) OceanPayNotify() {
	notifyData := this.Ctx.Input.RequestBody
	beego.Debug("OceanPayNotify notifyData:", string(notifyData))
	reqMap, err := oceanpayment.OceanPayMsgNotify(notifyData)
	if err != nil {
	    beego.Debug("OceanPayMsgNotify:", err.Error())
		return
	}
	if _, ok := reqMap["methods"]; !ok {
	    beego.Debug("OceanPayNotify message is invalid")
		return 
	}
	var payInfoCache *paylayer.PayInfoCache = new(paylayer.PayInfoCache)
	cacheKey := "payorder_" + reqMap["order_number"]
	redisconn := redisgo.GetInstance()
	if cacheValue, err := redisconn.GetString(cacheKey); err == nil && len(cacheValue) > 5  {
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			
		}
	}else{
	    beego.Debug("OceanPayNotify cache pay info err:", err.Error())
		return 
	}
	reqMap["LocalKind"] = "OCEANPAY_NOTIFY"
	reqMap["attach"] = payInfoCache.OrderKind
	imlayer.PushMsgSvrIns.PushDao <- &reqMap
	this.Ctx.WriteString("receive-ok")
	return
}
